package com.hulizuofang.wol.chat.client.presenter;

import java.util.logging.Level;
import java.util.logging.Logger;

import com.google.gwt.event.shared.EventBus;
import com.google.inject.Inject;
import com.gwtplatform.dispatch.shared.DispatchAsync;
import com.gwtplatform.mvp.client.View;
import com.gwtplatform.mvp.client.annotations.NameToken;
import com.gwtplatform.mvp.client.annotations.ProxyCodeSplit;
import com.gwtplatform.mvp.client.proxy.PlaceRequest;
import com.gwtplatform.mvp.client.proxy.ProxyPlace;
import com.hulizuofang.wol.chat.client.place.NameTokens;
import com.hulizuofang.wol.chat.client.ui.event.changeplace.ChangePlaceEvent;
import com.hulizuofang.wol.chat.client.ui.event.setlogin.SetLoginEvent;
import com.hulizuofang.wol.chat.client.ui.event.setlogin.SetLoginHandler;

public class StartPagePresenter extends AbstractNestedPresenter<StartPagePresenter.PageView, StartPagePresenter.PageProxy> {
	private static final Logger logger = Logger.getLogger(StartPagePresenter.class.getName());

	@ProxyCodeSplit
	@NameToken(NameTokens.START)
	public interface PageProxy extends ProxyPlace<StartPagePresenter> {
	}

	public interface PageView extends View {
		void addSetLoginHandler(SetLoginHandler handler);
	}

	@Inject
	public StartPagePresenter(EventBus eventBus, PageView view, PageProxy proxy, final DispatchAsync dispatcher) {
		super(eventBus, view, proxy, dispatcher);
	}

	private SetLoginHandler setLoginHandler = new SetLoginHandler() {
		@Override
		public void onSetLogin(SetLoginEvent event) {
			String login = event.getLogin();
			logger.log(Level.FINEST,"log in with '"+login+"'");
			ChangePlaceEvent.fire(getEventBus(),new PlaceRequest(NameTokens.WHOIS_ONLINE).with(NameTokens.LOGIN, login));
		}
	};

	@Override
	public void onBind() {
		getView().addSetLoginHandler(setLoginHandler);
	}

}
